package com.cyz.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/kill")
@Slf4j
public class SecKillController {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    ProductController productController;

    @GetMapping("start/{count}")
    public String start(@PathVariable int count){
        //将秒杀商品放入Redis
        redisTemplate.delete("remainingCount");//保证分布式锁可以返回true
        Boolean b = redisTemplate.opsForValue().setIfAbsent("remainingCount", count, 300, TimeUnit.SECONDS);
        System.out.println(b);
        log.info(String.valueOf(redisTemplate.opsForValue().get("remainingCount")));

        //更新数据库数量，并且更新MQ中的数量
        productController.start(count);

        return "存放Redis成功";
    }
}
